/*
 * @Author: wriliant 
 * @Date: 2019-06-28 10:03:15
 * @Description: 用户模块控制器
 * @Last Modified by: wriliant
 * @Last Modified time: 2019-06-28 10:03:15
 */

const path = require('path');
const operationSQL = require(path.join('../utils/dbconnect.js'))
const util = require(path.join('../utils/util.js'))


// 注册接口
const regist =  (request,respone)=>{
    const params = request.body;
    operationSQL.getPromise(" SELECT phone from user where phone = ? ",[params.phone]).then(result1=>{
        if (result1.length > 0) {
            operationSQL.getPromise(" SELECT * from user where phone = ? and status = ? ",[params.phone,0]).then(result2=>{
                result2.length > 0 ? util.returnMsg(7001, '该账户已存在，请直接登录', params.phone, respone) : util.returnMsg(7002, '该账户已冻结，请联系客服', params.phone, respone);
            })
        }else{
            let user_id = util.getUniqueId();
            let create_time = util.getNowFormatDate();
            operationSQL.getPromise(" insert into user(user_id,user_name,password,phone,email,id_card_no,birthday,invite_type,invite_phone,create_time) values(?,?,?,?,?,?,?,?,?,?) ", [user_id,params.user_name,params.password,params.phone,params.email,params.id_card_no,params.birthday,params.invite_type,params.invite_phone,create_time]).then(result3=>{
                if (result3.length > 0) {
                    operationSQL.getPromise("insert into user_role(belongto_user_id,user_tags) values(?,?)",[user_id,params.user_tags]).then(result4=>{
                        result4 ? util.returnMsg(0, '注册成功，请登录', params.phone, respone) : util.returnMsg(7003, '注册失败，请重新操作', params.phone, respone);
                    })
                }else{
                    util.returnMsg(7004, '注册失败，请重新操作', params.phone, respone);
                }
            })
        }
    });
}

// 登录接口
const login = (request,respone)=>{
    const params = request.body;
    operationSQL.getPromise(" SELECT phone from user where phone = ? ",[params.phone]).then(result1=>{
        if (result1.length > 0) {
            operationSQL.getPromise(" SELECT phone from user where phone = ? and status = 0 ",[params.phone]).then(result2=>{
                if (result2.length > 0) {
                    operationSQL.getPromise(" SELECT * from user where phone = ? and password = ? and status = 0 ",[params.phone,params.password]).then(result3=>{
                        if (result3.length > 0) {
                            let sessionID = result3[0].user_id;
                            request.session['"'+params.phone+'"'] = params.phone; // 登录成功，设置 session
                            let last_login_time = util.getNowFormatDate();
                            operationSQL.getPromise(" update user set last_login_time = ? where phone = ?", [last_login_time,params.phone]);
                            util.returnMsg(0, '登陆成功', result3[0], respone);
                        }else{
                            util.returnMsg(7005, '密码错误', params.phone, respone);
                        }
                    })
                }else{
                    util.returnMsg(7006, '该账户已被禁用，请联系客服', params.phone, respone);
                }
            })
        }else{
            util.returnMsg(7007, '该账户不是会员，请注册后登陆', params.phone, respone);
        }
    })
}

// 退出接口
const logout = (request,respone)=>{
    const params = request.body;
    delete request.session['"'+params.phone+'"'];
    util.returnMsg(0, '注销成功', false, respone);
}

// 修改密码
const updatePassword = (request,respone)=>{
    const params = request.body;
    operationSQL.getPromise(" SELECT * from user where phone = ? and password = ? ",[params.phone, params.oldPassword]).then(result1=>{
        if (result1.length > 0) {
            operationSQL.getPromise(" update user set password = ?  where phone = ? ",[params.newPassword, params.phone]).then(result2=>{
                result2.changedRows == 1 ? util.returnMsg(0, '密码修改成功',false, respone) : util.returnMsg(7008, '密码修改失败，请重新操作', false, respone);
            })
        }else{
            util.returnMsg(7009, '旧密码输入错误，请重新操作', false, respone)
        }
    })
}

// 修改个人资料
const updatePersonInfo = (request,respone)=>{
    const params = request.body;
    operationSQL.getPromise(" update user set user_name = ?, email = ?, id_card_no = ?, icon = ?, birthday = ? where phone = ?",[params.user_name,params.email,params.id_card_no,params.icon,params.birthday,params.phone]).then(result1=>{
        if (result1) {
            operationSQL.getPromise(" update user_role set user_tags = ? where belongto_user_id = ? ",[params.user_tags, params.user_id]).then(result2=>{
                if (result2) {
                    util.returnMsg(0, '修改成功', false, respone)
                }
            })
        }
    })
}

// 管理员查询用户列表
const findUserPageList = (request,respone)=>{
    
}

// 通过id查询用户
const findUserById = (request,respone)=>{
    
}

// 管理员修改用户
const updateUser = (request,respone)=>{
    
}

// 管理员删除用户
const delUserByUserPhone = (request,respone)=>{
    const params = request.body;
    operationSQL.getPromise(" update user set status = 1 where phone = ? ",[params.phone]).then(result2=>{
        result2.changedRows == 1 ? util.returnMsg(0, '会员删除成功',false, respone) : util.returnMsg(7008, '会员删除失败，请重新操作', false, respone);
    })
}

// 管理员重置用户密码
const resetUserPassword = (request,respone)=>{
    const params = request.body;
    operationSQL.getPromise(" update user set password = 000000 where phone = ? ",[params.phone]).then(result2=>{
        result2.changedRows == 1 ? util.returnMsg(0, '密码重置成功',false, respone) : util.returnMsg(7008, '密码重置失败，请重新操作', false, respone);
    })
}


module.exports = {
    regist,
    login,
    logout,
    updatePassword,
    updatePersonInfo,
    findUserPageList,
    findUserById,
    updateUser,
    delUserByUserPhone,
    resetUserPassword,
}